package web.Servlet;

import domain.student;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "testServlet" ,urlPatterns = "/chb")
public class testServlet  extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
             doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
                resp.setContentType("text/html;charset=UTF-8");
                 req.setAttribute("student",new student("万维聪聪", "wwc2020421"));
                 req.getRequestDispatcher("/dologin.jsp").forward(req,resp);



        /*  *//*手动添加cookie*//*
                resp.addCookie(new Cookie("chb","chbb"));

                *//*1.通过请求头来获取*//*
                String cookie = req.getHeader("Cookie");

                 System.out.println("通过请求头获取的cookie信息"+cookie);

                 *//*2.通过request方法*//*
                 Cookie[] cookies = req.getCookies();
                 if(cookie!=null){
                     for(int i=0;i<cookies.length;i++){
                         System.out.println("通过request封装好的方法获取cookied"+cookies[i].getName()+"--"+cookies[i].getValue());
                     }
                 }

        if(cookie==null){
                     resp.getWriter().print("欢迎新用户访问");
                 }else {

                         resp.getWriter().print("欢迎老用户访问");

                 }

        *//*     *//**//*1.请求行*//**//*
                 *//**//*资源路径*//**//*
                   String requestURI = req.getRequestURI();
                 System.out.println(requestURI);
                 *//**//*获取版本和协议*//**//*
               String protocol = req.getProtocol();
               *//**//*获取协议*//**//*
                 String scheme = req.getScheme();
              System.out.println(protocol+"分割--"+scheme);
                *//**//*getqueryString--get请求*//**//*
        String queryString = req.getQueryString();
        System.out.println(queryString);

        *//**//*获取请求体的参数*//**//*

        *//**//*获取请求头*//**//*
        String referer = req.getHeader("Referer");
        System.out.println(referer);
        Enumeration<String> headers = req.getHeaders("Accept");
        while (headers.hasMoreElements()){
            System.out.println(headers.nextElement()+"---");
        }



*//*



        *//*     *//**//* resp.getWriter().print("登录成功！啊啊啊,10秒后跳转刀登录成功页面!");
                 resp.setHeader("refresh","10,url='http://www.baidu.com'");*//**//*

                *//**//*手动重定向*//**//*
                *//**//*需要设置两个响应头*//**//*
       *//**//*         resp.setStatus(302);//302  代表重定向
                resp.setHeader("location","http://www.baidu.com");//设置响应头*//**//*
               *//**//* resp.sendRedirect("http://www.baidu.com");*//**//*
             *//**//*   resp.setHeader("Content-Disposition","attachment;filename=C:\\Users\\76053\\Desktop\\untitled\\web\\4.7.zip");*//**//*
                resp.getWriter().print("登录成功！啊啊啊");*/
    }
}
